const jwt = require('jsonwebtoken')
const secret = 'mynameisFantasyGao'; // 指定密钥，这是之后用来判断 token 合法性的标志

const userModel = require('./../models/user-info')

/**
 * 返回值
 * @param code 返回码
 * @param msg	返回信息
 * @return
 */

var resObj = (code,msg,token,resData) => {
  return {
    status: code,
    msg: msg,
    token:token,
    data:resData
  }
}

var logObj = (user,ip,msg) => {
  return {
    user: user,
    ip: ip,
    massage:msg
  }
}
/**
 *  用户信息
 */
// login
exports.USER_LOGIN_API = async(ctx,next)=>{
  let Info = ctx.request.query
  console.log(Info)
  if (!Info.user||!Info.password) {
    ctx.status = 200
    ctx.body = resObj(-1,'参数不全')
    return
  }
  let userObj = {}
  userObj.user = Info.user
  let userIp = ctx.request.ip.match(/\d+.\d+.\d+.\d+/)[0]
  let logInfo = logObj(Info.user,userIp,"登录系统")

  let resultData = await userModel.getOneByUserNameAndPassword({
    'password': logInfo.user.password,
    'name': logInfo.user.userName})

  if(resultData !=null){
    ctx.body = resObj(1,'登录成功',token)
  }else{
    ctx.body = resObj(2,'密码错误',token)
  }
}
